package cn.com.doone.common.uc.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.doone.common.uc.service.IRegisterService;
import cn.com.doone.common.uc.service.dto.UserInfo;
import cn.com.doone.common.uc.utils.MessageUtils;
import cn.com.doone.common.uc.utils.PasswordUtils;
import cn.com.doone.common.uc.utils.StringUtils;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/register/")
public class RegisterController {
	
	private static final Logger LOG = LoggerFactory.getLogger(RegisterController.class);
	
	@Autowired
	private IRegisterService registerService;
	
	/**
	 * 验证用户名
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("validUsername")
    @ResponseBody
    public Map<String, Object> validUsername(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//获取参数
		String userAccount = (String) request.getParameter("userAccount");
		
		//判断参数非空
		if(StringUtils.isNull(userAccount)) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
    		
		boolean isExist = registerService.validUsername(userAccount);
		
		if (isExist) {
			resultMap.put("success", false);
			resultMap.put("message", "该用户"+MessageUtils.ISEXIST);
			return resultMap;
		}
		
		resultMap.put("success", true);
		resultMap.put("message", "该用户"+MessageUtils.USEABLE);
    	
    	return resultMap;

    }
	
	/**
	 * 验证密码
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("validPassword")
    @ResponseBody
    public Map<String, Object> validPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//获取参数
		String password = (String) request.getParameter("password");
		
		//判断参数不为空
		if(StringUtils.isNull(password)) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		
		//判断弱密码
		String msg = PasswordUtils.checkPassword(password);
		if(StringUtils.isNotNull(msg)) {
			resultMap.put("success", false);
			resultMap.put("message", msg);
			return resultMap;
		}
		
		resultMap.put("success", true);
		resultMap.put("message", MessageUtils.USEABLE);
    	
    	return resultMap;

    }
	
	/**
	 * 添加用户
	 * 
	 * @param request
	 * @param json
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addUser")
    @ResponseBody
    public Map<String, Object> addUser(HttpServletRequest request) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String json = request.getParameter("json");
		
		if (json == null || json.isEmpty()) {
			resultMap.put("success", false);
			resultMap.put("message", "无效的数据！！");
		} else {
			UserInfo userInfo = JSON.parseObject(json, UserInfo.class);
			String tenantCode = request.getParameter("tenantCode");
			
			try {
				registerService.addUser(userInfo, tenantCode);
				resultMap.put("success", true);
				resultMap.put("message", "添加成功！！");
				
				LOG.debug("添加用户：" + userInfo);
			} catch (Exception e) {
				resultMap.put("success", false);
				resultMap.put("message", "添加失败！！");
				LOG.error("添加用户失败" + e);
			}
		}
    	
    	return resultMap;
    }
	
	@RequestMapping("removeUser")
    @ResponseBody
    public Map<String, Object> removeUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		String userAccount = (String) request.getParameter("userAccount");
    	
		try {
			registerService.removeUser(userAccount);
			resultMap.put("success", true);
			resultMap.put("message", "删除成功！！");
			
			LOG.debug("删除用户：" + userAccount);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", "删除失败！！");
			LOG.error("删除用户失败：" + e);
		}	
    	
    	return resultMap;

    }
	
	@RequestMapping("syncUser")
    @ResponseBody
    public Map<String, Object> userSync(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
    	
		try {
			registerService.syncUser();
			resultMap.put("success", true);
			resultMap.put("message", "同步成功！！");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", "同步失败！！");
		}	
    	
    	return resultMap;

    }
	
	@RequestMapping("registerUser")
    @ResponseBody
    public Map<String, Object> registerUser(HttpServletRequest request) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String json = request.getParameter("json");
		
		if (json == null || json.isEmpty()) {
			resultMap.put("success", false);
			resultMap.put("message", "无效的数据！！");
		} else {
			UserInfo userInfo = JSON.parseObject(json, UserInfo.class);
			String tenantName = request.getParameter("tenantName");
			
			try {
				String tenantCode = registerService.registerUser(userInfo, tenantName);
				resultMap.put("tenantCode", tenantCode);
				resultMap.put("success", true);
				resultMap.put("message", "添加成功！！");
			} catch (Exception e) {
				resultMap.put("success", false);
				resultMap.put("message", "添加失败！！");
			}
		}
    	
    	return resultMap;

    }
	
	@RequestMapping("resetPassword")
    @ResponseBody
    public Map<String, Object> resetPassword(HttpServletRequest request, @RequestBody String json) throws Exception {
    	
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//获取登陆账号
		final String username = request.getUserPrincipal().getName();
		if(!"doone_admin".equals(username)) {
			resultMap.put("success", false);
			resultMap.put("message", "只有超级管理员才能重置密码！！");
			return resultMap;
		}
		//获取参数
		String userAccount = request.getParameter("userAccount");
		
		//判断非空
		if(StringUtils.isNull(userAccount)) {
			resultMap.put("success", false);
			resultMap.put("message", MessageUtils.PARAMS_NULL);
			return resultMap;
		}
		
		try {
			
			String pwd = registerService.resetPassword(userAccount);
			
			resultMap.put("pwd", pwd);
			resultMap.put("success", true);
			resultMap.put("message", "修改成功！！");
			LOG.debug("重置密码:" + userAccount);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
			resultMap.put("message", "修改失败！！");
			LOG.debug("重置密码失败:" + e);
		}
    	
    	return resultMap;
    }
	

}
